package prototype;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.SwingConstants;

import java.awt.Font;

import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.awt.Component;

import javax.swing.Box;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.JButton;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Arrays;

public class Login extends JFrame implements KeyListener {

    private JPanel contentPane;
    private JTextField textField;
    private JPasswordField passwordField;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Login frame = new Login();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public Login() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        setLocationRelativeTo(this.getRootPane());
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        GridBagLayout gbl_contentPane = new GridBagLayout();
        gbl_contentPane.columnWidths = new int[]{190, 146, 0};
        gbl_contentPane.rowHeights = new int[]{34, 0, 14, 0, 0, 0, 0, 0};
        gbl_contentPane.columnWeights = new double[]{0.0, 0.0, Double.MIN_VALUE};
        gbl_contentPane.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
        contentPane.setLayout(gbl_contentPane);

        JLabel lblWelcome = new JLabel("Welcome! Please log in.");
        lblWelcome.setFont(new Font("Vijaya", Font.PLAIN, 33));
        lblWelcome.setHorizontalAlignment(SwingConstants.CENTER);
        GridBagConstraints gbc_lblWelcome = new GridBagConstraints();
        gbc_lblWelcome.anchor = GridBagConstraints.EAST;
        gbc_lblWelcome.gridwidth = 2;
        gbc_lblWelcome.insets = new Insets(0, 0, 5, 0);
        gbc_lblWelcome.gridx = 0;
        gbc_lblWelcome.gridy = 0;
        contentPane.add(lblWelcome, gbc_lblWelcome);

        Component verticalStrut = Box.createVerticalStrut(20);
        GridBagConstraints gbc_verticalStrut = new GridBagConstraints();
        gbc_verticalStrut.insets = new Insets(0, 0, 5, 5);
        gbc_verticalStrut.gridx = 0;
        gbc_verticalStrut.gridy = 1;
        contentPane.add(verticalStrut, gbc_verticalStrut);

        JLabel lblUsername = new JLabel("Username:");
        GridBagConstraints gbc_lblUsername = new GridBagConstraints();
        gbc_lblUsername.anchor = GridBagConstraints.EAST;
        gbc_lblUsername.insets = new Insets(0, 0, 5, 5);
        gbc_lblUsername.gridx = 0;
        gbc_lblUsername.gridy = 2;
        contentPane.add(lblUsername, gbc_lblUsername);

        textField = new JTextField();
        GridBagConstraints gbc_textField = new GridBagConstraints();
        gbc_textField.insets = new Insets(0, 0, 5, 0);
        gbc_textField.fill = GridBagConstraints.HORIZONTAL;
        gbc_textField.gridx = 1;
        gbc_textField.gridy = 2;
        contentPane.add(textField, gbc_textField);
        textField.setColumns(10);

        Component verticalStrut_1 = Box.createVerticalStrut(20);
        GridBagConstraints gbc_verticalStrut_1 = new GridBagConstraints();
        gbc_verticalStrut_1.insets = new Insets(0, 0, 5, 5);
        gbc_verticalStrut_1.gridx = 0;
        gbc_verticalStrut_1.gridy = 3;
        contentPane.add(verticalStrut_1, gbc_verticalStrut_1);

        JLabel lblPassword = new JLabel("Password:");
        GridBagConstraints gbc_lblPassword = new GridBagConstraints();
        gbc_lblPassword.anchor = GridBagConstraints.EAST;
        gbc_lblPassword.insets = new Insets(0, 0, 5, 5);
        gbc_lblPassword.gridx = 0;
        gbc_lblPassword.gridy = 4;
        contentPane.add(lblPassword, gbc_lblPassword);

        passwordField = new JPasswordField();
        GridBagConstraints gbc_textField_1 = new GridBagConstraints();
        gbc_textField_1.insets = new Insets(0, 0, 5, 0);
        gbc_textField_1.fill = GridBagConstraints.HORIZONTAL;
        gbc_textField_1.gridx = 1;
        gbc_textField_1.gridy = 4;
        contentPane.add(passwordField, gbc_textField_1);
        passwordField.setColumns(10);

        Component verticalStrut_2 = Box.createVerticalStrut(20);
        GridBagConstraints gbc_verticalStrut_2 = new GridBagConstraints();
        gbc_verticalStrut_2.insets = new Insets(0, 0, 5, 0);
        gbc_verticalStrut_2.gridx = 1;
        gbc_verticalStrut_2.gridy = 5;
        contentPane.add(verticalStrut_2, gbc_verticalStrut_2);

        textField.addKeyListener(this);
        passwordField.addKeyListener(this);

        JButton btnNewButton = new JButton("OK");
        btnNewButton.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent arg0) {
                if (textField.getText().equals("myUser")) {
                    char[] input = passwordField.getPassword();
                    char[] correctPassword = {'m','y','P','a','s','s'};
                    if (Arrays.equals(input, correctPassword)) {
                        loginSuccessful();
                    }
                }
            }
        });
        GridBagConstraints gbc_btnNewButton = new GridBagConstraints();
        gbc_btnNewButton.anchor = GridBagConstraints.EAST;
        gbc_btnNewButton.gridx = 1;
        gbc_btnNewButton.gridy = 6;
        contentPane.add(btnNewButton, gbc_btnNewButton);
    }

    private void loginSuccessful() {
        this.dispose();
        new MainApp().setVisible(true);
    }

    public void keyPressed(KeyEvent e) {
        if (e.getKeyCode() == KeyEvent.VK_ENTER)
            if (textField.getText().equals("myUser")) {
                char[] input = passwordField.getPassword();
                char[] correctPassword = {'m','y','P','a','s','s'};
                if (Arrays.equals(input, correctPassword)) {
                    loginSuccessful();
                }
            }
    }

    public void keyReleased(KeyEvent e) {}
    public void keyTyped(KeyEvent e) {}

}
